package com.lidx.back.statistics.rest.feedback;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;

import com.lidx.back.statistics.commons.Constants;
import com.lidx.back.statistics.commons.MaterialUpdateConfig;
import com.lidx.back.statistics.commons.SpringContextHolder;
import com.lidx.back.statistics.entity.FeedBack;
import com.lidx.back.statistics.rest.VersionBean;
import com.lidx.back.statistics.utils.FTPUtils;
import com.lidx.back.statistics.utils.FileUtils;
public class FeedbackInputCheck {
	

	public FeedBack checkInput(FeedbackRequest request) throws Exception{
		String content = request.getContent();
		List<String> images = request.getImages();
		if(StringUtils.isEmpty(content))
			throw new Exception("评论内容不能为空");
		FeedBack feedBack = new FeedBack();
		feedBack.setContent(content);
		if(!StringUtils.isEmpty(request.getQq()))
			feedBack.setQq(request.getQq());
		if(!StringUtils.isEmpty(request.getPhone())){
			feedBack.setPhone(request.getPhone());
		}
		StringBuffer sb;
		File imageFile = new File(Constants.IMAGE_FILE_PATH);
		if(images.size()>0){
			
			if(!imageFile.exists()){
				imageFile.mkdir();
			}
			String[] imageNames =  new String[images.size()] ;
			int num = 0;
			//传进来base64的图片数据
			for(String imageBase64 : images){
				imageNames[num] = System.currentTimeMillis()+".jpg";
				byte[] bytes = Base64.decodeBase64(imageBase64);
				imageFile = new File("d://pictures//"+imageNames[num]);
				FileImageOutputStream fiout = new FileImageOutputStream(imageFile);
				fiout.write(bytes,0,bytes.length);
				fiout.close();
				
				FileInputStream is = new FileInputStream(imageFile);
				MaterialUpdateConfig config = SpringContextHolder.getBean("materialUpdateConfig");
				FTPUtils.uploadFile(config.getIisFtpHost(), 21, config.getFtpUserName(), 
						config.getFtpPassword(), Constants.IIS_URL_FEEDBACKIMAGES_ROOT,
						imageNames[num], is);
				num++;
			}
			sb = new StringBuffer();
			for(int i=0;i<imageNames.length;i++){
				if(i == imageNames.length-1){
					sb.append(imageNames[i]);
				}else{
					sb.append(imageNames[i]+",");
				}
			}
			feedBack.setImageNames(sb.toString());
		}
		VersionBean versionBean = request.getVersion();
		if(null != versionBean){
			feedBack.setVersionNum(versionBean.getVersionNumber());
			feedBack.setVersionType(versionBean.getVersionType());
		}
		return feedBack;
	}
	
	/*public static void main(String[] args) throws IOException {
		File file = new File("d://cheninfo//test2.jpg");
		FileImageInputStream fin = new FileImageInputStream(file); 
		byte[] bytes  = new byte[1024];  
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		int numBytesRead = 0;
      while ((numBytesRead = fin.read(bytes)) != -1) {
      output.write(bytes, 0, numBytesRead);
      }
      byte[] data = output.toByteArray();
      output.close();
      fin.close();
		String base64 = Base64.encodeBase64String(output.toByteArray());
		System.out.println(base64);
	}*/
}
